﻿//using System.Collections;
using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class Heavenly_Dragon : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        //private bool Listening = false;//是否没有执行完invoke相关操作   
        //private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private static bool Gamma_adjust_timeout_flag = false;

        string[] Mura_Rowdata_Array; //保存每个拍摄灰阶的整面CSV数据
        private Encoding encoding;        //编码
        //private List<string> CSVType;
        public List<string> Line_Rowdata = new List<string>();//保存每个拍摄灰阶的整面的每一行CSV数据

        public int Udc_type = 1; //% 0->692UDC  1->639UDC
        public int Num_of_grayscale = 8;  //拍摄灰阶的数量定义
        public double[,] Sub_GrayScale_Ave = new double[8, 3]; //保存副屏区CSV数据的均值
        public double[,] Main_GrayScale_Ave = new double[8, 3];//保存主屏区CSV数据的自定义范围内（可整面）的均值
        public double[,] Ref_CapRas_RGB_Ave = new double[3, 3];//保存主屏区中心300*300CSV计算均值
        public int[] Demura_GrayScale = new int[] { 32, 64, 96, 128, 160, 192, 224, 255 };
        public byte[] Simulation_GrayScale = new byte[] { 64, 192, 255 }; //模拟仿真图灰阶
        public byte[] ImgBytes_Musk; //保存Musk图原始字节

        public byte[] ImageData0;
        public byte[] ImageData1;
        public byte[] ImageData2;

        //-----------------------------------------
        frmMain frmHelp = null;
        public Heavenly_Dragon(frmMain Dragon)
        {
            frmHelp = Dragon;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体    
        }

        private void Heavenly_Dragon_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 载入主副屏合成后的CSV路径
        /// </summary>
        public string defaultfilePath = "";
        public string Default_Path = "";
        private void Btn_load_csv_path_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.SelectedPath = defaultfilePath;
            path.ShowDialog();
            CSV_Data_Path.Text = path.SelectedPath;
            defaultfilePath = path.SelectedPath;
            Default_Path = path.SelectedPath;

            this.listBox_CSVFile.Items.Clear();
            string imgtype = "*.CSV";
            string[] ImageType = imgtype.Split('|');
            for (int i = 0; i < ImageType.Length; i++)
            {
                string[] tmp1 = System.IO.Directory.GetFiles(path.SelectedPath, ImageType[i]);
                foreach (string s in tmp1)
                {
                    listBox_CSVFile.Items.Add(new FileInfo(s).Name);
                }
            }
        }

        /// <summary>
        /// 载入主副屏合成后的CSV路径，实际数据载入
        /// </summary>
        private void Btn_Load_CSV_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listBox_CSVFile.Items.Count; i++)
            {
                listBox_CSVFile.SelectedIndex = i;
                string filePath = defaultfilePath + "\\" + listBox_CSVFile.SelectedItem;
                CsvStreamReader(filePath);

                progressBar1.Value = listBox_CSVFile.SelectedIndex * 100 / (listBox_CSVFile.Items.Count - 1);
                Percent.Text = progressBar1.Value.ToString() + "%";
                Application.DoEvents();
            }
        }
        public void CsvStreamReader(string fileName)
        {
            // this.encoding = Encoding.Default;
            LoadCsvFile(fileName);
        }
        /// <summary>
        /// 载入CSV文件
        /// </summary>
        private void LoadCsvFile(string fileName)
        {
            int UDC_Vactive = Int16.Parse(tBx_Vactive.Text);
            //对数据的有效性进行验证
            if (fileName == null)
            {
                throw new Exception("请指定要载入的CSV文件名");
            }
            else if (!File.Exists(fileName))
            {
                throw new Exception("指定的CSV文件不存在");
            }
            else
            {
            }
            if (this.encoding == null)
            {
                this.encoding = Encoding.Default;
            }
            StreamReader sr = new StreamReader(fileName, this.encoding);
            // string csvDataLine;
            //csvDataLine = "";
            while (true)
            {
                string fileDataLine;
                fileDataLine = sr.ReadLine();
                if (fileDataLine == null)
                {
                    break;
                }
                else
                {
                    Line_Rowdata.Add(fileDataLine);//循环添加元素
                }
            }
            sr.Close();
            Mura_Rowdata_Array = Line_Rowdata.ToArray();//strArray=[str0,str1,str2]
            Line_Rowdata.Clear();//清空buffer
            if (Mura_Rowdata_Array.Length != UDC_Vactive)//若数据量不正确 =2340 & 2460
            {
                MessageBox.Show(listBox_CSVFile.SelectedItem + "数据加载不正确!");
            }
            switch (listBox_CSVFile.SelectedItem)
            {
                case "CapRas_032_R.CSV": UDC_Setting.CapRas.CapRas_032_R = Mura_Rowdata_Array; break;
                case "CapRas_032_G.CSV": UDC_Setting.CapRas.CapRas_032_G = Mura_Rowdata_Array; break;
                case "CapRas_032_B.CSV": UDC_Setting.CapRas.CapRas_032_B = Mura_Rowdata_Array; break;
                case "CapRas_064_R.CSV": UDC_Setting.CapRas.CapRas_064_R = Mura_Rowdata_Array; break;
                case "CapRas_064_G.CSV": UDC_Setting.CapRas.CapRas_064_G = Mura_Rowdata_Array; break;
                case "CapRas_064_B.CSV": UDC_Setting.CapRas.CapRas_064_B = Mura_Rowdata_Array; break;
                case "CapRas_096_R.CSV": UDC_Setting.CapRas.CapRas_096_R = Mura_Rowdata_Array; break;
                case "CapRas_096_G.CSV": UDC_Setting.CapRas.CapRas_096_G = Mura_Rowdata_Array; break;
                case "CapRas_096_B.CSV": UDC_Setting.CapRas.CapRas_096_B = Mura_Rowdata_Array; break;
                case "CapRas_128_R.CSV": UDC_Setting.CapRas.CapRas_128_R = Mura_Rowdata_Array; break;
                case "CapRas_128_G.CSV": UDC_Setting.CapRas.CapRas_128_G = Mura_Rowdata_Array; break;
                case "CapRas_128_B.CSV": UDC_Setting.CapRas.CapRas_128_B = Mura_Rowdata_Array; break;
                case "CapRas_160_R.CSV": UDC_Setting.CapRas.CapRas_160_R = Mura_Rowdata_Array; break;
                case "CapRas_160_G.CSV": UDC_Setting.CapRas.CapRas_160_G = Mura_Rowdata_Array; break;
                case "CapRas_160_B.CSV": UDC_Setting.CapRas.CapRas_160_B = Mura_Rowdata_Array; break;
                case "CapRas_192_R.CSV": UDC_Setting.CapRas.CapRas_192_R = Mura_Rowdata_Array; break;
                case "CapRas_192_G.CSV": UDC_Setting.CapRas.CapRas_192_G = Mura_Rowdata_Array; break;
                case "CapRas_192_B.CSV": UDC_Setting.CapRas.CapRas_192_B = Mura_Rowdata_Array; break;
                case "CapRas_224_R.CSV": UDC_Setting.CapRas.CapRas_224_R = Mura_Rowdata_Array; break;
                case "CapRas_224_G.CSV": UDC_Setting.CapRas.CapRas_224_G = Mura_Rowdata_Array; break;
                case "CapRas_224_B.CSV": UDC_Setting.CapRas.CapRas_224_B = Mura_Rowdata_Array; break;
                case "CapRas_255_R.CSV": UDC_Setting.CapRas.CapRas_255_R = Mura_Rowdata_Array; break;
                case "CapRas_255_G.CSV": UDC_Setting.CapRas.CapRas_255_G = Mura_Rowdata_Array; break;
                case "CapRas_255_B.CSV": UDC_Setting.CapRas.CapRas_255_B = Mura_Rowdata_Array; break;


                case "CapRas_032_R.csv": UDC_Setting.CapRas.CapRas_032_R = Mura_Rowdata_Array; break;
                case "CapRas_032_G.csv": UDC_Setting.CapRas.CapRas_032_G = Mura_Rowdata_Array; break;
                case "CapRas_032_B.csv": UDC_Setting.CapRas.CapRas_032_B = Mura_Rowdata_Array; break;
                case "CapRas_064_R.csv": UDC_Setting.CapRas.CapRas_064_R = Mura_Rowdata_Array; break;
                case "CapRas_064_G.csv": UDC_Setting.CapRas.CapRas_064_G = Mura_Rowdata_Array; break;
                case "CapRas_064_B.csv": UDC_Setting.CapRas.CapRas_064_B = Mura_Rowdata_Array; break;
                case "CapRas_096_R.csv": UDC_Setting.CapRas.CapRas_096_R = Mura_Rowdata_Array; break;
                case "CapRas_096_G.csv": UDC_Setting.CapRas.CapRas_096_G = Mura_Rowdata_Array; break;
                case "CapRas_096_B.csv": UDC_Setting.CapRas.CapRas_096_B = Mura_Rowdata_Array; break;
                case "CapRas_128_R.csv": UDC_Setting.CapRas.CapRas_128_R = Mura_Rowdata_Array; break;
                case "CapRas_128_G.csv": UDC_Setting.CapRas.CapRas_128_G = Mura_Rowdata_Array; break;
                case "CapRas_128_B.csv": UDC_Setting.CapRas.CapRas_128_B = Mura_Rowdata_Array; break;
                case "CapRas_160_R.csv": UDC_Setting.CapRas.CapRas_160_R = Mura_Rowdata_Array; break;
                case "CapRas_160_G.csv": UDC_Setting.CapRas.CapRas_160_G = Mura_Rowdata_Array; break;
                case "CapRas_160_B.csv": UDC_Setting.CapRas.CapRas_160_B = Mura_Rowdata_Array; break;
                case "CapRas_192_R.csv": UDC_Setting.CapRas.CapRas_192_R = Mura_Rowdata_Array; break;
                case "CapRas_192_G.csv": UDC_Setting.CapRas.CapRas_192_G = Mura_Rowdata_Array; break;
                case "CapRas_192_B.csv": UDC_Setting.CapRas.CapRas_192_B = Mura_Rowdata_Array; break;
                case "CapRas_224_R.csv": UDC_Setting.CapRas.CapRas_224_R = Mura_Rowdata_Array; break;
                case "CapRas_224_G.csv": UDC_Setting.CapRas.CapRas_224_G = Mura_Rowdata_Array; break;
                case "CapRas_224_B.csv": UDC_Setting.CapRas.CapRas_224_B = Mura_Rowdata_Array; break;
                case "CapRas_255_R.csv": UDC_Setting.CapRas.CapRas_255_R = Mura_Rowdata_Array; break;
                case "CapRas_255_G.csv": UDC_Setting.CapRas.CapRas_255_G = Mura_Rowdata_Array; break;
                case "CapRas_255_B.csv": UDC_Setting.CapRas.CapRas_255_B = Mura_Rowdata_Array; break;

                case "R32.csv": UDC_Setting.CapRas.CapRas_032_R = Mura_Rowdata_Array; break;
                case "G32.csv": UDC_Setting.CapRas.CapRas_032_G = Mura_Rowdata_Array; break;
                case "B32.csv": UDC_Setting.CapRas.CapRas_032_B = Mura_Rowdata_Array; break;
                case "R64.csv": UDC_Setting.CapRas.CapRas_064_R = Mura_Rowdata_Array; break;
                case "G64.csv": UDC_Setting.CapRas.CapRas_064_G = Mura_Rowdata_Array; break;
                case "B64.csv": UDC_Setting.CapRas.CapRas_064_B = Mura_Rowdata_Array; break;
                case "R96.csv": UDC_Setting.CapRas.CapRas_096_R = Mura_Rowdata_Array; break;
                case "G96.csv": UDC_Setting.CapRas.CapRas_096_G = Mura_Rowdata_Array; break;
                case "B96.csv": UDC_Setting.CapRas.CapRas_096_B = Mura_Rowdata_Array; break;
                case "R128.csv": UDC_Setting.CapRas.CapRas_128_R = Mura_Rowdata_Array; break;
                case "G128.csv": UDC_Setting.CapRas.CapRas_128_G = Mura_Rowdata_Array; break;
                case "B128.csv": UDC_Setting.CapRas.CapRas_128_B = Mura_Rowdata_Array; break;
                case "R160.csv": UDC_Setting.CapRas.CapRas_160_R = Mura_Rowdata_Array; break;
                case "G160.csv": UDC_Setting.CapRas.CapRas_160_G = Mura_Rowdata_Array; break;
                case "B160.csv": UDC_Setting.CapRas.CapRas_160_B = Mura_Rowdata_Array; break;
                case "R192.csv": UDC_Setting.CapRas.CapRas_192_R = Mura_Rowdata_Array; break;
                case "G192.csv": UDC_Setting.CapRas.CapRas_192_G = Mura_Rowdata_Array; break;
                case "B192.csv": UDC_Setting.CapRas.CapRas_192_B = Mura_Rowdata_Array; break;
                case "R224.csv": UDC_Setting.CapRas.CapRas_224_R = Mura_Rowdata_Array; break;
                case "G224.csv": UDC_Setting.CapRas.CapRas_224_G = Mura_Rowdata_Array; break;
                case "B224.csv": UDC_Setting.CapRas.CapRas_224_B = Mura_Rowdata_Array; break;
                case "R255.csv": UDC_Setting.CapRas.CapRas_255_R = Mura_Rowdata_Array; break;
                case "G255.csv": UDC_Setting.CapRas.CapRas_255_G = Mura_Rowdata_Array; break;
                case "B255.csv": UDC_Setting.CapRas.CapRas_255_B = Mura_Rowdata_Array; break;

                default: MessageBox.Show(listBox_CSVFile.SelectedItem + "CapRas赋值不正确!"); break;
            }
        }

        // <summary>
        // 从CSV中提取副屏区域的String值，并存入buf
        // 并根据RGB mask，滤除副屏区buf内的无效数据
        // </summary>
        private void MASK_639UDC_Region_Data(object sender, EventArgs e)
        {
            //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            int state = 0;//状态机 - MASK映射; Follow Matlab程序 [64*48]
            for (int y = 0; y < 64; y++) //y0~y1
            {
                if (state == 4)
                { state = 1; }
                else
                { state = state + 1; }
                for (int x = 0; x < 48; x++) //x0~x1
                {
                    switch (state)
                    {
                        case 1: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        case 2: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        case 3: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            break;
                        case 4: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        default:
                            state = 0;
                            break;
                    }
                }
            }
        }
        private void MASK_692UDC_Region_Data(object sender, EventArgs e)
        {
            //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            int state = 0;//状态机 - MASK映射; Follow Matlab程序 [80*48]
            for (int y = 0; y < 80; y++) //y0~y1
            {
                if (state == 4)
                { state = 1; }
                else
                { state = state + 1; }
                for (int x = 0; x < 48; x++) //x0~x1
                {
                    switch (state)
                    {
                        case 1: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        case 2: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        case 3: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            break;
                        case 4: //行数
                            if (x % 2 == 0)//偶数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] = 0;
                            }
                            else //奇数列
                            {
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] = 0;
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x] = 0; UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] = 0;
                            }
                            break;
                        default:
                            state = 0;
                            break;
                    }
                }
            }
        }
        // <summary>
        // 从CSV中提取副屏区域的String值，并存入buf
        // </summary>
        private void Average_UDC_Region_Data(object sender, EventArgs e)
        {
            double sum_r = 0, sum_g = 0, sum_b = 0;
            int count_r = 0, count_g = 0, count_b = 0;
            int udc_area_x = int.Parse(tBx_UDC_x1.Text) - int.Parse(tBx_UDC_x0.Text) + 1; //%%UDC 范围设定
            int udc_area_y = int.Parse(tBx_UDC_y1.Text) - int.Parse(tBx_UDC_y0.Text) + 1;
            double[,] udc_capras_tep_r = new double[udc_area_y, udc_area_x];
            double[,] udc_capras_tep_g = new double[udc_area_y, udc_area_x];
            double[,] udc_capras_tep_b = new double[udc_area_y, udc_area_x];

            for (int g = 0; g < Demura_GrayScale.Length; g++)
            { //变更灰阶重置
                sum_r = 0; sum_g = 0; sum_b = 0;
                count_r = 0; count_g = 0; count_b = 0;
                switch (Demura_GrayScale[g])
                {
                    case 32:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B;
                        break;
                    case 64:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B;
                        break;
                    case 96:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B;
                        break;
                    case 128:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B;
                        break;
                    case 160:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B;
                        break;
                    case 192:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B;
                        break;
                    case 224:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B;
                        break;
                    case 255:
                        udc_capras_tep_r = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R;
                        udc_capras_tep_g = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G;
                        udc_capras_tep_b = UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B;
                        break;
                    default: MessageBox.Show("udc_capras_tep 赋值有误！"); break;
                }
                //%滤除0，转换为一维数组取均值
                foreach (double douArr in udc_capras_tep_r)
                {
                    if (douArr != 0)
                    {
                        sum_r = sum_r + douArr;
                        count_r = count_r + 1;
                    }
                }
                foreach (double douArr in udc_capras_tep_g)
                {
                    if (douArr != 0)
                    {
                        sum_g = sum_g + douArr;
                        count_g = count_g + 1;
                    }
                }
                foreach (double douArr in udc_capras_tep_b)
                {
                    if (douArr != 0)
                    {
                        sum_b = sum_b + douArr;
                        count_b = count_b + 1;
                    }
                }
                Sub_GrayScale_Ave[g, 0] = sum_r / count_r;
                Sub_GrayScale_Ave[g, 1] = sum_g / count_g;
                Sub_GrayScale_Ave[g, 2] = sum_b / count_b;
            }
        }

        // <summary>
        // UDC矩阵剔除0元素，并存入一维数组
        // </summary>
        private void UDC_Array_Data_Process(object sender, EventArgs e)
        {
            int udc_area_x = int.Parse(tBx_UDC_x1.Text) - int.Parse(tBx_UDC_x0.Text) + 1; //%%UDC 范围设定
            int udc_area_y = int.Parse(tBx_UDC_y1.Text) - int.Parse(tBx_UDC_y0.Text) + 1;

            for (int g = 0; g < Simulation_GrayScale.Length; g++)
            { //变更灰阶重置
                for (int y = 0; y < udc_area_y; y++)
                {
                    for (int x = 0; x < udc_area_x; x++)
                    {
                        switch (Simulation_GrayScale[g])
                        {
                            case 64:
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_R.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_G.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_B.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x]);
                                }
                                break;

                            case 192:
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_R.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_G.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_B.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x]);
                                }
                                break;

                            case 255:
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_R.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_G.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x]);
                                }
                                if (UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x] != 0)
                                {
                                    UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_B.Add(UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x]);
                                }
                                break;
                        }
                    }
                }
            }
        }


        // <summary>
        // 从CSV中提取副屏局部区域的String值，并存入buf
        // </summary>
        private void UDC_Region_Data_Convert(object sender, EventArgs e)
        {
            string[] arr_r = new string[] { };
            string[] arr_g = new string[] { };
            string[] arr_b = new string[] { };
            int udc_area_x = Int16.Parse(tBx_UDC_x1.Text) - Int16.Parse(tBx_UDC_x0.Text) + 1; //%%UDC 范围设定
            int udc_area_y = Int16.Parse(tBx_UDC_y1.Text) - Int16.Parse(tBx_UDC_y0.Text) + 1;

            int UDC_Range_X0 = Int16.Parse(tBx_UDC_x0.Text);
            int UDC_Range_X1 = Int16.Parse(tBx_UDC_x1.Text);
            int UDC_Range_Y0 = Int16.Parse(tBx_UDC_y0.Text);
            int UDC_Range_Y1 = Int16.Parse(tBx_UDC_y1.Text);
            int UDC_Hactive = Int16.Parse(tBx_Hactive.Text);
            int UDC_Vactive = Int16.Parse(tBx_Vactive.Text);

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B = new double[udc_area_y, udc_area_x];

            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G = new double[udc_area_y, udc_area_x];
            UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B = new double[udc_area_y, udc_area_x];

            //提取各副屏区矩阵
            for (int g = 0; g < Demura_GrayScale.Length; g++)  //=8
            {   //提取副屏区矩阵
                for (int y = UDC_Range_Y0 - 1; y < UDC_Range_Y1; y++) //逐行提取
                {
                    switch (Demura_GrayScale[g])
                    {
                        case 32:
                            arr_r = UDC_Setting.CapRas.CapRas_032_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_032_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_032_B[y].Split(',');
                            break;
                        case 64:
                            arr_r = UDC_Setting.CapRas.CapRas_064_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_064_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_064_B[y].Split(',');
                            break;
                        case 96:
                            arr_r = UDC_Setting.CapRas.CapRas_096_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_096_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_096_B[y].Split(',');
                            break;
                        case 128:
                            arr_r = UDC_Setting.CapRas.CapRas_128_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_128_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_128_B[y].Split(',');
                            break;
                        case 160:
                            arr_r = UDC_Setting.CapRas.CapRas_160_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_160_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_160_B[y].Split(',');
                            break;
                        case 192:
                            arr_r = UDC_Setting.CapRas.CapRas_192_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_192_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_192_B[y].Split(',');
                            break;
                        case 224:
                            arr_r = UDC_Setting.CapRas.CapRas_224_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_224_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_224_B[y].Split(',');
                            break;
                        case 255:
                            arr_r = UDC_Setting.CapRas.CapRas_255_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_255_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_255_B[y].Split(',');
                            break;
                        default:
                            MessageBox.Show("灰阶不存在！");
                            break;
                    }
                    int x_tep = 0;
                    for (int x = UDC_Range_X0 - 1; x < UDC_Range_X1; x++) //提取特定列
                    {
                        switch (Demura_GrayScale[g])
                        {
                            case 32:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_032_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 64:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_064_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 96:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_096_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 128:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_128_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 160:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_160_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 192:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_192_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 224:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_224_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            case 255:
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_R[y, x_tep] = double.Parse(arr_r[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_G[y, x_tep] = double.Parse(arr_g[x]);
                                UDC_Setting.UDC_Csv_Matrix.UDC_CapRas_255_B[y, x_tep] = double.Parse(arr_b[x]);
                                break;
                            default:
                                MessageBox.Show("灰阶不存在！");
                                break;
                        }
                        x_tep++;
                    }
                }
            }
            if (Udc_type == 0)//%692UDC
            {
                MASK_692UDC_Region_Data(null, null);
            }
            else if (Udc_type == 1)//%639UDC
            {
                MASK_639UDC_Region_Data(null, null);
            }

            Average_UDC_Region_Data(null, null);
            UDC_Array_Data_Process(null, null);
        }

        // <summary>
        // 从CSV中提取主屏局部区域的String值，并存入buf
        // 计算主屏各灰阶的均值
        // </summary>
        private void Main_Region_Data_Convert(object sender, EventArgs e)
        {
            string[] arr_r = new string[] { };
            string[] arr_g = new string[] { };
            string[] arr_b = new string[] { };

            int Main_area_x = int.Parse(tBx_Main_x1.Text) - int.Parse(tBx_Main_x0.Text) + 1;
            int Main_area_y = int.Parse(tBx_Main_y1.Text) - int.Parse(tBx_Main_y0.Text) + 1; //%64

            int Main_ave_ragion_X0 = int.Parse(tBx_Main_x0.Text);
            int Main_ave_ragion_X1 = int.Parse(tBx_Main_x1.Text);
            int Main_ave_ragion_Y0 = int.Parse(tBx_Main_y0.Text);
            int Main_ave_ragion_Y1 = int.Parse(tBx_Main_y1.Text);

            double[,] main_capras_tep_r = new double[Main_area_y, Main_area_x];
            double[,] main_capras_tep_g = new double[Main_area_y, Main_area_x];
            double[,] main_capras_tep_b = new double[Main_area_y, Main_area_x];

            double sum_r = 0, sum_g = 0, sum_b = 0;
            int count_r = 0, count_g = 0, count_b = 0;

            //[720*2340]
            int y_tep = 0;
            //提取各主屏区局部矩阵
            for (int g = 0; g < Demura_GrayScale.Length; g++)
            {//提取主屏区局部（范围自定义，可覆盖整个AA区）矩阵
                y_tep = 0;
                for (int y = Main_ave_ragion_Y0; y < Main_ave_ragion_Y1; y++) //128行开始
                {
                    switch (Demura_GrayScale[g])
                    {
                        case 32:
                            arr_r = UDC_Setting.CapRas.CapRas_032_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_032_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_032_B[y].Split(',');
                            break;
                        case 64:
                            arr_r = UDC_Setting.CapRas.CapRas_064_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_064_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_064_B[y].Split(',');
                            break;
                        case 96:
                            arr_r = UDC_Setting.CapRas.CapRas_096_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_096_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_096_B[y].Split(',');
                            break;
                        case 128:
                            arr_r = UDC_Setting.CapRas.CapRas_128_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_128_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_128_B[y].Split(',');
                            break;
                        case 160:
                            arr_r = UDC_Setting.CapRas.CapRas_160_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_160_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_160_B[y].Split(',');
                            break;
                        case 192:
                            arr_r = UDC_Setting.CapRas.CapRas_192_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_192_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_192_B[y].Split(',');
                            break;
                        case 224:
                            arr_r = UDC_Setting.CapRas.CapRas_224_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_224_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_224_B[y].Split(',');
                            break;
                        case 255:
                            arr_r = UDC_Setting.CapRas.CapRas_255_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_255_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_255_B[y].Split(',');
                            break;
                        default:
                            MessageBox.Show("主屏-灰阶不存在1！");
                            break;
                    }
                    int x_tep = 0;
                    for (int x = Main_ave_ragion_X0; x < Main_ave_ragion_X1; x++)
                    {
                        main_capras_tep_r[y_tep, x_tep] = double.Parse(arr_r[x]);
                        main_capras_tep_g[y_tep, x_tep] = double.Parse(arr_g[x]);
                        main_capras_tep_b[y_tep, x_tep] = double.Parse(arr_b[x]);
                        x_tep++;
                    }
                    y_tep++;
                }
                //Average_Main_Region_Data(g);
                sum_r = 0; sum_g = 0; sum_b = 0;
                count_r = 0; count_g = 0; count_b = 0;

                foreach (double douArr in main_capras_tep_r)
                {
                    sum_r = sum_r + douArr;
                    count_r = count_r + 1;
                }
                foreach (double douArr in main_capras_tep_g)
                {
                    sum_g = sum_g + douArr;
                    count_g = count_g + 1;
                }
                foreach (double douArr in main_capras_tep_b)
                {
                    sum_b = sum_b + douArr;
                    count_b = count_b + 1;
                }
                Main_GrayScale_Ave[g, 0] = sum_r / count_r;
                Main_GrayScale_Ave[g, 1] = sum_g / count_g;
                Main_GrayScale_Ave[g, 2] = sum_b / count_b;
            }
        }

        // <summary>
        // 计算主副屏均值的比值的离散程度，并存入Post文件
        // 显示主副屏各自的均值
        // 计算副屏区理想的校准系数
        // </summary>
        private void Diff_Cal_of_UDC_With_Main(object sender, EventArgs e)
        {
            UDC_Setting.Csv_Ratio.UDC_Ratio_R = new double[Num_of_grayscale];
            UDC_Setting.Csv_Ratio.UDC_Ratio_G = new double[Num_of_grayscale];
            UDC_Setting.Csv_Ratio.UDC_Ratio_B = new double[Num_of_grayscale];

            for (int i = 0; i < Num_of_grayscale; i++) //%[32, 64, 96, 128, 160, 192, 224, 255 ]
            {
                UDC_Setting.Csv_Ratio.UDC_Ratio_R[i] = Sub_GrayScale_Ave[i, 0] / Main_GrayScale_Ave[i, 0];//%主副屏均值比值 
                UDC_Setting.Csv_Ratio.UDC_Ratio_G[i] = Sub_GrayScale_Ave[i, 1] / Main_GrayScale_Ave[i, 1];
                UDC_Setting.Csv_Ratio.UDC_Ratio_B[i] = Sub_GrayScale_Ave[i, 2] / Main_GrayScale_Ave[i, 2];
            }

            R32_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[0].ToString();
            G32_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[0].ToString();
            B32_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[0].ToString();

            R64_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[1].ToString();
            G64_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[1].ToString();
            B64_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[1].ToString();

            R96_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[2].ToString();
            G96_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[2].ToString();
            B96_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[2].ToString();

            R128_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[3].ToString();
            G128_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[3].ToString();
            B128_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[3].ToString();

            R160_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[4].ToString();
            G160_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[4].ToString();
            B160_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[4].ToString();

            R192_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[5].ToString();
            G192_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[5].ToString();
            B192_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[5].ToString();

            R224_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[6].ToString();
            G224_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[6].ToString();
            B224_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[6].ToString();

            R255_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_R[7].ToString();
            G255_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_G[7].ToString();
            B255_Diff.Text = UDC_Setting.Csv_Ratio.UDC_Ratio_B[7].ToString();
        }

        // <summary>
        // 从CSV中提取主屏中心300*300的数据均值，并存入buf
        // </summary>
        private void UDC_Region_Cal_ReferData(object sender, EventArgs e)
        {
            string[] arr_r = new string[] { };
            string[] arr_g = new string[] { };
            string[] arr_b = new string[] { };

            int ref_x_range = 300;
            int ref_y_range = 300;

            int ref_area_x = Int16.Parse(tBx_Hactive.Text) / 2 - ref_x_range / 2 + 1; //%%UDC 范围设定
            int ref_area_y = Int16.Parse(tBx_Vactive.Text) / 2 - ref_y_range / 2 + 1;

            double[,] Ref_CapRas_R = new double[ref_y_range, ref_x_range];
            double[,] Ref_CapRas_G = new double[ref_y_range, ref_x_range];
            double[,] Ref_CapRas_B = new double[ref_y_range, ref_x_range];

            //提取各副屏区矩阵
            for (int g = 0; g < Simulation_GrayScale.Length; g++)  //=8
            {
                double sum_r = 0, sum_g = 0, sum_b = 0;
                int count_r = 0, count_g = 0, count_b = 0;
                int y_tep = 0;
                for (int y = ref_area_y; y < ref_area_y + ref_y_range; y++) //逐行提取
                {
                    switch (Simulation_GrayScale[g])
                    {
                        case 64:
                            arr_r = UDC_Setting.CapRas.CapRas_064_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_064_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_064_B[y].Split(',');
                            break;
                        case 128:
                            arr_r = UDC_Setting.CapRas.CapRas_128_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_128_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_128_B[y].Split(',');
                            break;
                        case 192:
                            arr_r = UDC_Setting.CapRas.CapRas_192_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_192_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_192_B[y].Split(',');
                            break;
                        case 255:
                            arr_r = UDC_Setting.CapRas.CapRas_255_R[y].Split(',');
                            arr_g = UDC_Setting.CapRas.CapRas_255_G[y].Split(',');
                            arr_b = UDC_Setting.CapRas.CapRas_255_B[y].Split(',');
                            break;
                        default:
                            MessageBox.Show("灰阶不存在！");
                            break;
                    }
                    int x_tep = 0;
                    for (int x = ref_area_x; x < ref_area_x + ref_x_range; x++) //提取特定列
                    {
                        Ref_CapRas_R[y_tep, x_tep] = double.Parse(arr_r[x]);
                        Ref_CapRas_G[y_tep, x_tep] = double.Parse(arr_g[x]);
                        Ref_CapRas_B[y_tep, x_tep] = double.Parse(arr_b[x]);
                        x_tep++;
                    }
                    y_tep++;
                }
                foreach (double douArr in Ref_CapRas_R)
                {
                    sum_r = sum_r + douArr;
                    count_r = count_r + 1;
                }
                foreach (double douArr in Ref_CapRas_G)
                {
                    sum_g = sum_g + douArr;
                    count_g = count_g + 1;
                }
                foreach (double douArr in Ref_CapRas_B)
                {
                    sum_b = sum_b + douArr;
                    count_b = count_b + 1;
                }
                Ref_CapRas_RGB_Ave[g, 0] = sum_r / count_r;
                Ref_CapRas_RGB_Ave[g, 1] = sum_g / count_g;
                Ref_CapRas_RGB_Ave[g, 2] = sum_b / count_b;
            }
        }

        /// <summary>
        /// 导入离散系数至RGB调节控件
        /// </summary>
        private void Dragon_Load_Org_Diff_Click(object sender, EventArgs e)
        {
            Dragon_R0.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_R[1] * 100);
            Dragon_G0.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_G[1] * 100);
            Dragon_B0.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_B[1] * 100);

            Dragon_R1.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_R[5] * 100);
            Dragon_G1.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_G[5] * 100);
            Dragon_B1.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_B[5] * 100);

            Dragon_R2.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_R[7] * 100);
            Dragon_G2.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_G[7] * 100);
            Dragon_B2.Value = (int)(UDC_Setting.Csv_Ratio.UDC_Ratio_B[7] * 100);
        }
        /// <summary>
        /// 计算并生成主副屏离散曲线
        /// </summary>
        private void Btn_CSV_Convert_to_offset_Click(object sender, EventArgs e)
        {
            try
            {
                UDC_Region_Data_Convert(null, null);//计算副屏区均值
                Main_Region_Data_Convert(null, null);//计算主屏区局部均值
                Diff_Cal_of_UDC_With_Main(null, null); //计算主副屏离散程度
            }
            catch
            {
                MessageBox.Show("CSV数据未加载!");
            }
        }

        //------------CSV to bmp Convert-------\

        private void CSV_Update_to_UDC_pic(object sender, EventArgs e)
        {

            Button btnTmp = (Button)sender;

            double r_row, g_row, b_row;

            int imageWidth = int.Parse(tBx_Hactive.Text);
            int imageHeight = int.Parse(tBx_Vactive.Text) / 6;

            int UDC_Range_X0 = Int16.Parse(tBx_UDC_x0.Text);
            int UDC_Range_X1 = Int16.Parse(tBx_UDC_x1.Text);
            int UDC_Range_Y0 = Int16.Parse(tBx_UDC_y0.Text);
            int UDC_Range_Y1 = Int16.Parse(tBx_UDC_y1.Text);

            double ratio_udc_r = 1, ratio_udc_g = 1, ratio_udc_b = 1;

            //{ 32, 64, 96, 128, 160, 192, 224, 255 };
            //Sub_GrayScale_Ave = new double[8, 3]; //保存副屏区CSV数据的均值
            int pix_count = 0;

            //[720*2340]
            for (int g = 0; g < Simulation_GrayScale.Length; g++)
            {
                switch (Simulation_GrayScale[g])
                {
                    case 64:
                        ratio_udc_r = Main_GrayScale_Ave[1, 0] * (double)(Dragon_R0.Value / 100);
                        ratio_udc_g = Main_GrayScale_Ave[1, 1] * (double)(Dragon_G0.Value / 100);
                        ratio_udc_b = Main_GrayScale_Ave[1, 2] * (double)(Dragon_B0.Value / 100); //64
                        break;
                    case 192:
                        ratio_udc_r = Main_GrayScale_Ave[5, 0] * (double)(Dragon_R1.Value / 100);
                        ratio_udc_g = Main_GrayScale_Ave[5, 1] * (double)(Dragon_G1.Value / 100);
                        ratio_udc_b = Main_GrayScale_Ave[5, 2] * (double)(Dragon_B1.Value / 100); //192
                        break;
                    case 255:
                        ratio_udc_r = Main_GrayScale_Ave[7, 0] * (double)(Dragon_R2.Value / 100);
                        ratio_udc_g = Main_GrayScale_Ave[7, 1] * (double)(Dragon_G2.Value / 100);
                        ratio_udc_b = Main_GrayScale_Ave[7, 2] * (double)(Dragon_B2.Value / 100); //255
                        break;
                }
                pix_count = 0;
                int org_count = 0;
                for (int y = UDC_Range_Y0 - 1; y < UDC_Range_Y1; y++) //只处理透明区对应行
                {
                    for (int x = 0; x < imageWidth; x++)
                    {
                        if (x >= UDC_Range_X0 - 1 && x < UDC_Range_X1)
                        {
                            if (ImgBytes_Musk[54 + pix_count] != 0) //MASK有效位置，1驱动上下左右四像素
                            {
                                switch (Simulation_GrayScale[g])
                                {
                                    case 64:
                                        ImageData0[pix_count] = (byte)((byte)((double)(ratio_udc_r / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_R[org_count])) * Simulation_GrayScale[0]) & ImgBytes_Musk[54 + pix_count]);//fseek(54)
                                        ImageData0[pix_count + 1] = (byte)((byte)((double)(ratio_udc_g / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_G[org_count])) * Simulation_GrayScale[0]) & ImgBytes_Musk[54 + pix_count + 1]);
                                        ImageData0[pix_count + 2] = (byte)((byte)((double)(ratio_udc_b / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_064_B[org_count])) * Simulation_GrayScale[0]) & ImgBytes_Musk[54 + pix_count + 2]);  //64
                                        break;
                                    case 192:
                                        ImageData1[pix_count] = (byte)((byte)((double)(ratio_udc_r / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_R[org_count])) * Simulation_GrayScale[1]) & ImgBytes_Musk[54 + pix_count]);
                                        ImageData1[pix_count + 1] = (byte)((byte)((double)(ratio_udc_g / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_G[org_count])) * Simulation_GrayScale[1]) & ImgBytes_Musk[54 + pix_count + 1]);
                                        ImageData1[pix_count + 2] = (byte)((byte)((double)(ratio_udc_b / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_192_B[org_count])) * Simulation_GrayScale[1]) & ImgBytes_Musk[54 + pix_count + 2]);  //192
                                        break;
                                    case 255:
                                        r_row = (double)((double)(ratio_udc_r / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_R[org_count])) * Simulation_GrayScale[2]);
                                        g_row = (double)((double)(ratio_udc_g / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_G[org_count])) * Simulation_GrayScale[2]);
                                        b_row = (double)((double)(ratio_udc_b / (double)(UDC_Setting.UDC_Csv_Matrix.UDC_Origin_255_B[org_count])) * Simulation_GrayScale[2]); //255
                                        ImageData2[pix_count] = (byte)(r_row < 255 ? r_row : 255);
                                        ImageData2[pix_count + 1] = (byte)(g_row < 255 ? g_row : 255);
                                        ImageData2[pix_count + 2] = (byte)(b_row < 255 ? b_row : 255);
                                        break;
                                    default:
                                        MessageBox.Show("!!");
                                        break;
                                }
                                org_count++;
                            }
                        }
                        //填pixel
                        pix_count = pix_count + 3;
                    }

                }
                //数组转换为Intptr
                IntPtr pData = Marshal.AllocHGlobal(imageWidth * imageHeight * 3); //托管空间
                int lll = 0;

                switch (Simulation_GrayScale[g])
                {
                    case 64:
                        Marshal.Copy(ImageData0, 0, pData, imageWidth * imageHeight * 3);
                        break;
                    case 192:
                        lll = 1;
                        Marshal.Copy(ImageData1, 0, pData, imageWidth * imageHeight * 3);
                        break;
                    case 255:
                        Marshal.Copy(ImageData2, 0, pData, imageWidth * imageHeight * 3);
                        break;
                    default:
                        MessageBox.Show("bmp灰阶不存在!!");
                        break;
                }
                Bitmap bmp = new Bitmap(imageWidth, imageHeight, imageWidth * 3, PixelFormat.Format24bppRgb, pData);
                if (lll == 1)
                {
                    pictureBox3.Image = bmp;
                }
                //string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
                //bmp.Save(directory + "\\save.bmp", ImageFormat.Bmp);
                switch (Simulation_GrayScale[g])
                {
                    case 64:
                        bmp.Save("64_local.bmp", ImageFormat.Bmp);
                        break;
                    case 192:
                        bmp.Save("192_local.bmp", ImageFormat.Bmp);
                        break;
                    case 255:
                        bmp.Save("255_local.bmp", ImageFormat.Bmp);
                        break;
                    default:
                        MessageBox.Show("bmp灰阶不存在!!");
                        break;
                }
            }
        }

        private void CSV_Convert_to_Emulate_Pic(object sender, EventArgs e)///(object sender, EventArgs e)
        {
            double sum_r = 0, sum_g = 0, sum_b = 0;
            int count_r = 0, count_g = 0, count_b = 0;
            double r_row, g_row, b_row;

            string[] arr_r = new string[] { };
            string[] arr_g = new string[] { };
            string[] arr_b = new string[] { };

            int Main_area_x = int.Parse(tBx_Hactive.Text);
            int Main_area_y = int.Parse(tBx_Vactive.Text);

            byte[,] Main_Dragon_R = new byte[Main_area_y, Main_area_x];
            byte[,] Main_Dragon_G = new byte[Main_area_y, Main_area_x];
            byte[,] Main_Dragon_B = new byte[Main_area_y, Main_area_x];

            int imageWidth = int.Parse(tBx_Hactive.Text);
            int imageHeight = int.Parse(tBx_Vactive.Text);

            ImageData0 = new byte[imageWidth * 3 * imageHeight];
            ImageData1 = new byte[imageWidth * 3 * imageHeight];
            ImageData2 = new byte[imageWidth * 3 * imageHeight];

            int pix_count = 0;
            try
            {
                //[720*2340]
                for (int g = 0; g < Simulation_GrayScale.Length; g++)
                {
                    pix_count = 0;
                    for (int y = 0; y < Main_area_y; y++) //行开始
                    {
                        switch (Simulation_GrayScale[g])
                        {
                            case 32:
                                arr_r = UDC_Setting.CapRas.CapRas_032_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_032_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_032_B[y].Split(',');
                                break;
                            case 64:
                                arr_r = UDC_Setting.CapRas.CapRas_064_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_064_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_064_B[y].Split(',');
                                break;
                            case 96:
                                arr_r = UDC_Setting.CapRas.CapRas_096_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_096_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_096_B[y].Split(',');
                                break;
                            case 128:
                                arr_r = UDC_Setting.CapRas.CapRas_128_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_128_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_128_B[y].Split(',');
                                break;
                            case 160:
                                arr_r = UDC_Setting.CapRas.CapRas_160_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_160_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_160_B[y].Split(',');
                                break;
                            case 192:
                                arr_r = UDC_Setting.CapRas.CapRas_192_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_192_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_192_B[y].Split(',');
                                break;
                            case 224:
                                arr_r = UDC_Setting.CapRas.CapRas_224_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_224_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_224_B[y].Split(',');
                                break;
                            case 255:
                                arr_r = UDC_Setting.CapRas.CapRas_255_R[y].Split(',');
                                arr_g = UDC_Setting.CapRas.CapRas_255_G[y].Split(',');
                                arr_b = UDC_Setting.CapRas.CapRas_255_B[y].Split(',');
                                break;
                            default:
                                MessageBox.Show("主屏-灰阶不存在1！");
                                break;
                        }
                        for (int x = 0; x < Main_area_x; x++)
                        {
                            switch (Simulation_GrayScale[g])
                            {
                                case 64:
                                    Main_Dragon_R[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[0, 0] / double.Parse(arr_r[x]))) * Simulation_GrayScale[0]);
                                    Main_Dragon_G[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[0, 1] / double.Parse(arr_g[x]))) * Simulation_GrayScale[0]);
                                    Main_Dragon_B[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[0, 2] / double.Parse(arr_b[x]))) * Simulation_GrayScale[0]);  //64
                                                                                                                                                                     //--------------------------------------------------------------------------------------------------
                                    ImageData0[pix_count] = (byte)(Main_Dragon_R[y, x] & ImgBytes_Musk[54 + pix_count]); //fseek(54)
                                    ImageData0[pix_count + 1] = (byte)(Main_Dragon_G[y, x] & ImgBytes_Musk[54 + pix_count + 1]);
                                    ImageData0[pix_count + 2] = (byte)(Main_Dragon_B[y, x] & ImgBytes_Musk[54 + pix_count + 2]); //64
                                    break;
                                case 192:
                                    Main_Dragon_R[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[1, 0] / double.Parse(arr_r[x]))) * Simulation_GrayScale[1]);
                                    Main_Dragon_G[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[1, 1] / double.Parse(arr_g[x]))) * Simulation_GrayScale[1]);
                                    Main_Dragon_B[y, x] = (byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[1, 2] / double.Parse(arr_b[x]))) * Simulation_GrayScale[1]); //192
                                                                                                                                                                    //--------------------------------------------------------------------------------------------------
                                    ImageData1[pix_count] = (byte)(Main_Dragon_R[y, x] & ImgBytes_Musk[54 + pix_count]); //fseek(54)
                                    ImageData1[pix_count + 1] = (byte)(Main_Dragon_G[y, x] & ImgBytes_Musk[54 + pix_count + 1]);
                                    ImageData1[pix_count + 2] = (byte)(Main_Dragon_B[y, x] & ImgBytes_Musk[54 + pix_count + 2]); //192
                                    break;
                                case 255:
                                    r_row = Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 0] / double.Parse(arr_r[x]))) * Simulation_GrayScale[2];
                                    g_row = Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 1] / double.Parse(arr_g[x]))) * Simulation_GrayScale[2];
                                    b_row = Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 2] / double.Parse(arr_b[x]))) * Simulation_GrayScale[2]; //255
                                                                                                                                              //--------------------------------------------------------------------------------------------------
                                    Main_Dragon_R[y, x] = (byte)(r_row < 255 ? r_row : 255);         //(byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 0] / double.Parse(arr_r[x]))) * Simulation_GrayScale[2]);
                                    Main_Dragon_G[y, x] = (byte)(g_row < 255 ? g_row : 255);         //(byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 1] / double.Parse(arr_g[x]))) * Simulation_GrayScale[2]);
                                    Main_Dragon_B[y, x] = (byte)(b_row < 255 ? b_row : 255);         //(byte)(Math.Sqrt((double)(Ref_CapRas_RGB_Ave[2, 2] / double.Parse(arr_b[x]))) * Simulation_GrayScale[2]); //255
                                                                                                     //--------------------------------------------------------------------------------------------------
                                    ImageData2[pix_count] = (byte)(Main_Dragon_R[y, x] & ImgBytes_Musk[54 + pix_count]); //fseek(54)
                                    ImageData2[pix_count + 1] = (byte)(Main_Dragon_G[y, x] & ImgBytes_Musk[54 + pix_count + 1]);
                                    ImageData2[pix_count + 2] = (byte)(Main_Dragon_B[y, x] & ImgBytes_Musk[54 + pix_count + 2]); //192
                                    break;
                                default:
                                    MessageBox.Show("!!");
                                    break;
                            }
                            //填pixel

                            pix_count = pix_count + 3;
                        }
                    }
                    //数组转换为Intptr
                    IntPtr pData = Marshal.AllocHGlobal(imageWidth * imageHeight * 3); //托管空间
                                                                                       //Marshal.Copy(ImgBytes_Musk, 54, pData, imageWidth * imageHeight * 3);
                    switch (Simulation_GrayScale[g])
                    {
                        case 64:
                            Marshal.Copy(ImageData0, 0, pData, imageWidth * imageHeight * 3);
                            break;
                        case 192:
                            Marshal.Copy(ImageData1, 0, pData, imageWidth * imageHeight * 3);
                            break;
                        case 255:
                            Marshal.Copy(ImageData2, 0, pData, imageWidth * imageHeight * 3);
                            break;
                        default:
                            MessageBox.Show("ImageData不存在!!");
                            break;
                    }

                    Bitmap bmp = new Bitmap(imageWidth, imageHeight, imageWidth * 3, PixelFormat.Format24bppRgb, pData);
                    pictureBox2.Image = bmp;
                    //string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
                    //bmp.Save(directory + "\\save.bmp", ImageFormat.Bmp);
                    switch (Simulation_GrayScale[g])
                    {
                        case 64:
                            bmp.Save("64.bmp", ImageFormat.Bmp);
                            break;
                        case 192:
                            bmp.Save("192.bmp", ImageFormat.Bmp);
                            break;
                        case 255:
                            bmp.Save("255.bmp", ImageFormat.Bmp);
                            break;
                        default:
                            MessageBox.Show("bmp灰阶不存在!!");
                            break;

                    }
                }
            }
            catch
            {
                MessageBox.Show("请载入musk图！");
            }
        }

        private void Dragon_Load_Org_Bmp_Click(object sender, EventArgs e)
        {
            UDC_Region_Cal_ReferData(null, null);//计算中心点目标值300*300
            CSV_Convert_to_Emulate_Pic(null, null);//CSV_TO_bmp
        }
        /// <summary>  


        string defaultfilePath0;
        private void Pic_Musk_Load_Click(object sender, EventArgs e)
        {
            string muskPath = "";
            //判断配置文件是否存在
            if (muskPath != "")
            {
                defaultfilePath0 = muskPath;
            }

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = muskPath;
            openFileDialog.Filter = "定位图|*.bmp|所有文件|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                muskPath = System.IO.Path.GetFullPath(openFileDialog.FileName);//得到路径
                                                                               //   strPath += "\\";
                FileStream fileStream = new FileStream(muskPath, FileMode.Open, FileAccess.Read);
                pictureBox1.Image = Image.FromStream(fileStream);

                ImgBytes_Musk = new byte[fileStream.Length];
                fileStream.Read(ImgBytes_Musk, 0, ImgBytes_Musk.Length);

                fileStream.Close();
                fileStream.Dispose(); //单击击 预览 BMP图片


                Bitmap b = new Bitmap(openFileDialog.FileName);
                MemoryStream ms = new MemoryStream();
                b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以，至于区别么，下面有解释
                ms.Close();

                /*
                                BinaryReader br = new BinaryReader(fileStream);
                                ImgBytes_Musk = null;
                                ImgBytes_Musk = br.ReadBytes((int)fileStream.Length); //将流读入到字节数组中

                                byte[] ImgBytes_Musk_First = new byte[54];
                                Array.Copy(ImgBytes_Musk,0, ImgBytes_Musk_First,0,54);

                                byte[] ImgBytes_Musk_Second = new byte[Int16.Parse(tBx_Hactive.Text)* Int16.Parse(tBx_Vactive.Text) * 3 - 54];
                                Array.Copy(ImgBytes_Musk, 54, ImgBytes_Musk_Second, 0, Int16.Parse(tBx_Hactive.Text) * Int16.Parse(tBx_Vactive.Text) * 3 - 54);
                                Array.Reverse(ImgBytes_Musk_Second);

                                Array.Clear(ImgBytes_Musk,0, ImgBytes_Musk.Length);
                                ImgBytes_Musk_First.CopyTo(ImgBytes_Musk, 0);
                                ImgBytes_Musk_Second.CopyTo(ImgBytes_Musk, ImgBytes_Musk_First.Length);
                */
                // return imgBytesIn;

            }
            else
            {

            }
        }


        //---------------------绑点调节----------------------------------
        private void Dragon_Red_Click(object sender, EventArgs e)
        {
            CSV_Update_to_UDC_pic(null, null);
        }

        private void Dragon_Green_Click(object sender, EventArgs e)
        {

        }

        private void Dragon_Blue_Click(object sender, EventArgs e)
        {

        }

        private void BtnAutoGammas_Dragon_Click(object sender, EventArgs e)
        {

        }

        private void Heavenly_Dragon_gamma(Object myObject, EventArgs e)
        {
            int total_points = 2;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }

            }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index == total_points)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Heavenly_Dragon gamma调试完毕!\r\n";
                }
                if (index < total_points)
                {
                    /*
                    if (NT37800A_AOD_Inter_cbx.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }
                    */
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 70;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t;
                lumg = frmHelp.CASDK_LUM * Lg_t;
                lumb = frmHelp.CASDK_LUM * Lb_t;

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], Dragon_R2.Value, Dragon_G2.Value, Dragon_B2.Value, Simulation_GrayScale[2 - index], 0x1ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                Dragon_R2.Value = r;
                                Dragon_G2.Value = g;
                                Dragon_B2.Value = b;
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], Dragon_R1.Value, Dragon_G1.Value, Dragon_B1.Value, Simulation_GrayScale[2 - index], 0x1ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                Dragon_R1.Value = r;
                                Dragon_G1.Value = g;
                                Dragon_B2.Value = b;
                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], Dragon_R0.Value, Dragon_G0.Value, Dragon_B0.Value, Simulation_GrayScale[2 - index], 0x1ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                Dragon_R0.Value = r;
                                Dragon_G0.Value = g;
                                Dragon_B0.Value = b;
                            }
                            break;
                        default:
                            break;
                    }

                    CSV_Update_to_UDC_pic(null, null);
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }

        private void BtnCalculateDragon_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d;
            //Text_Processing(Gamma_SetBox.Text);//S提取
            string[] arr = Dragon_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToInt32(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }

            try  //获取调整spec
            {
                txtDragon_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtDragon_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtDragon_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txtDragon_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtDragon_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtDragon_x.Text = Diven_Gamma[0].X.ToString();
                txtDragon_y.Text = Diven_Gamma[0].Y.ToString();

                for (int p = 0; p < Total_points; p++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[p] = (Math.Pow((Diven_Offset[p].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                }

                CaSDKObj.YUV_to_RGB(Diven_Gamma[0].Y, Diven_Gamma[0].X, out Lr_d, out Lb_d, out Lg_d);//R/B/G分亮度转换

                for (int i = 0; i < Total_points; i++)
                {
                    D_lums[i, 0] = D_lum[i] * Lr_d; //R/B/G分亮度计算
                    D_lums[i, 1] = D_lum[i] * Lg_d;
                    D_lums[i, 2] = D_lum[i] * Lb_d;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }

        private void Btn_Dragon_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }
    }
}
